﻿using System;
using System.Web.UI.WebControls;

namespace Carbon.Web.Forms.jQueryForms
{
    public class SimpleEditor : jQueryFormsViewControlBase
    {
		#region Fields (12)
		private bool m_AddDefaultContentboxes = false;
		private bool m_ShowDate = default(bool);
		private bool m_ShowTime = default(bool);

		private int? m_MaxLength = default(int?);

		private String m_FloatFormat = "{0:#,0.00}";
		private String m_IntFormat = "{0}";
		private String m_LabelText = default(String);
		private String m_ValidationRegex = default(String);
		private String m_ValidationRegexErrorMessage = default(String);

		private TextBoxMode m_TextMode = default(TextBoxMode);

		private Unit m_Height = default(Unit);
		private Unit m_Width = default(Unit);
		#endregion

		#region Properties (13)
		public virtual bool AddDefaultContainerBoxes
        {
            get
            {
                return m_AddDefaultContentboxes;
            }
            set
            {
                m_AddDefaultContentboxes = false;
            }
        }
		public Type BasicType
        {
            get
            {
                return InternalUtility.ExtractTypeWithoutNullable(Binder.Property.PropertyType);
            }
        }
		public String FloatFormat
        {
            get
            {
                return m_FloatFormat;
            }
            set
            {
                m_FloatFormat = value;
            }
        }
		public Unit Height
        {
            get
            {
                return m_Height;
            }
            set
            {
                m_Height = value;
            }
        }
		public String IntFormat
        {
            get
            {
                return m_IntFormat;
            }
            set
            {
                m_IntFormat = value;
            }
        }
		public String LabelText
        {
            get
            {
                return m_LabelText;
            }
            set
            {
                m_LabelText = value;
            }
        }
		public int? MaxLength
        {
            get
            {
                return m_MaxLength;
            }
            set
            {
                m_MaxLength = value;
            }
        }
		public bool ShowDate
        {
            get
            {
                return m_ShowDate;
            }
            set
            {
                m_ShowDate = value;
            }
        }
		public bool ShowTime
        {
            get
            {
                return m_ShowTime;
            }
            set
            {
                m_ShowTime = value;
            }
        }
		public TextBoxMode TextMode
        {
            get
            {
                return m_TextMode;
            }
            set
            {
                m_TextMode = value;
            }
        }
		public String ValidationRegex
        {
            get
            {
                return m_ValidationRegex;
            }
            set
            {
                m_ValidationRegex = value;
            }
        }
		public virtual String ValidationRegexErrorMessage
        {
            get
            {
                return m_ValidationRegexErrorMessage;
            }
            set
            {
                m_ValidationRegexErrorMessage = value;
            }
        }
		public Unit Width
        {
            get
            {
                return m_Width;
            }
            set
            {
                m_Width = value;
            }
        }
		#endregion

		#region Constructors (1)
		public SimpleEditor()
        {
        }
		#endregion

		#region ASP Processing

		public override void RenderControl(System.Web.UI.HtmlTextWriter writer)
        {
            if (AddDefaultContainerBoxes)
            {
                RenderBoxes(writer, ErrorDuringUpdateModel, ShowErrorText, ErrorTextDuringUpdateModel, () =>
                    {
                        base.RenderControl(writer);
                    });
            }
            else
            {
                base.RenderControl(writer);
            }
        }

		#endregion

		#region Methods (3)

		protected override IEditor CreateEditor()
        {
            Type basicType = BasicType;

            if (basicType == typeof(int))
                return new NumberEditor(this);
            else if (basicType == typeof(long))
                return new NumberEditor(this);
            else if (basicType == typeof(float))
                return new NumberEditor(this);
            else if (basicType == typeof(double))
                return new NumberEditor(this);
            else if (basicType == typeof(decimal))
                return new NumberEditor(this);
            else if (basicType == typeof(String))
                return new TextBoxEditor(this);
            else if (basicType == typeof(DateTime))
                return new DateTimeEditor(this);
            else if (basicType == typeof(bool))
                return new BooleanEditor(this);
            else
                throw new InvalidOperationException(string.Format("Unknown type '{0}'", basicType.FullName));
        }

		public override void OnUpdateModel(object sender, UpdateModelEventArgs args)
        {
            try
            {
                base.OnUpdateModel(sender, args);
            }
            catch (Exception)
            {
                this.ErrorDuringUpdateModel = true;
                this.ErrorTextDuringUpdateModel = Resources.GetString("Error.FormatException");
            }
        }

		public override void Validate()
        {
            base.Validate();

            if (ErrorDuringUpdateModel)
                IsValid = false;
        }

		#endregion
    }
}